package com.hspedu.seckill.controller;

import com.hspedu.seckill.model.result.RespBean;
import com.hspedu.seckill.model.vo.LoginVo;
import com.hspedu.seckill.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author: xujiabing
 * @date: 2024-10-03 11:56
 * @description 登录控制层
 */
@Controller
@Slf4j
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 转发到 templates/login.html 静态页面
     *
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 处理用户登录请求
     *
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/doLogin")
    public RespBean doLogin(@Valid LoginVo loginVo,
                            HttpServletRequest request,
                            HttpServletResponse response) {
        log.info("接收到loginVo = {}", loginVo);
        return userService.doLogin(loginVo, request, response);
    }
}
